<?php

class Cardtype_Model extends CI_Model {
	
	function Cardtype_Model() {
		parent::__construct ();
		$this->load->database ();
	}
	
	function get($id = FALSE) {
		if ($id === FALSE) {
			$query = $this->db->get ( 'cardtype' );
			if ($query->num_rows() <= 0){
				return FALSE;
			}
			return $query->result_array ();
		} else {
			$query = $this->db->get_where ( 'cardtype', array ('id' => $id ) );
			if ($query->num_rows() <= 0){
				return FALSE;
			}
			return $query->row_array ();
		}
	}
	
	function add($data) {
		
		if (! isset ( $data ['name'] ) || strlen ( $data ['name'] ) < 3 || strlen ( $data ['name'] ) > 30) {
			return 0;
		}
		foreach ( $data as &$d ) {
			$d = $this->db->escape_str ( $d );
		}
		
		$this->db->where ( 'name', $data ['name'] );
		$query = $this->db->get ( 'cardtype' );
		if ($query->num_rows () > 0)
			return false;
		
		if (! $this->db->insert ( 'cardtype', $data ))
			return 0;
		else {
			return $this->db->insert_id ();
		}
	}
	
	/*
     * 
     */
	
	function delete($id) {
		$this->db->where ( 'id', $id );
		$query = $this->db->delete ( 'cardtype' );
		if (! $query)
			return false;
		else
			return true;
	}
	
	/* Function: Update
     * Author: Chung HD
     * Date: 27/09/2011
     */
	
	function update($id, $data) {
		foreach ( $data as &$d ) {
			$d = $this->db->escape_str ( $d );
		}
		
		if (isset ( $data ['name'] ))
			$newdata ['name'] = $data ['name'];
		if (isset ( $data ['description'] ))
			$newdata ['description'] = $data ['description'];
		if (isset ( $data ['status'] ))
			$newdata ['status'] = $data ['status'];
		
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'cardtype' );
		if ($query->num_rows () == 0)
			return false;
		else {
			$this->db->where ( 'id', $id );
			if ($this->db->update ( 'cardtype', $newdata ))
				return true;
			else
				return false;
		}
	}
	
/*
     * Function: searchcardtypeByLetter($letter)
     * Author: Chung Hd
     * Date: 28/09/2011
     */
}

?>